//
//  TopUpViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/28.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TopUpViewController.h"
#import "TopUpAndWithdrawView.h"
#import "TopUpResultViewController.h"

@interface TopUpViewController ()
/**
 充值view
 */
@property (nonatomic , strong) TopUpAndWithdrawView *topUpView;
/**
 充值方式
 */
@property (nonatomic , assign) PayType payType;

@end

@implementation TopUpViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.navTitleLabel.text = @"充值";
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.topUpView];
}


- (void) initLayout {
    [self.topUpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(TJ_NavBarHeight);
    }];
}


- (void) initAction {
    @weakify(self);
    [self.topUpView setWechatBlock:^{
        @strongify(self);
        self.topUpView.wechatSelectImg.image = [UIImage imageNamed:@"adopt_buy_selected"];
        self.topUpView.alipaySelectImg.image = [UIImage imageNamed:@"meat_cart_unchecked"];
        self.payType = PayTypeWechat;
    }];
    [self.topUpView setAlipayBlock:^{
        @strongify(self);
        self.topUpView.wechatSelectImg.image = [UIImage imageNamed:@"meat_cart_unchecked"];
        self.topUpView.alipaySelectImg.image = [UIImage imageNamed:@"adopt_buy_selected"];
        self.payType = PayTypeAlipay;
    }];
    
    [self.topUpView.confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)initData {
    
    self.topUpView.title.text = @"充值金额";
    self.topUpView.payTypeTitle.text = @"选择充值方式";
    [self.topUpView.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
}

#pragma mark - delegate

#pragma mark - target action
- (void)confirmBtnClick {
    NSString *topUpMoney = self.topUpView.moneyInputField.text;
//    if (topUpMoney.length == 0 || [topUpMoney rangeOfString:@"."].location != NSNotFound) {
//        [self toastWithContent:@"请输入整数倍充值金额"];
//        return;
//    }
//    if (!self.payType) {
//        [self toastWithContent:@"请选择充值方式"];
//        return;
//    }
//    [[PayInputView sharedInstance] display];
//    @weakify(self);
//    [[PayInputView sharedInstance] setInputCompleteBlock:^(SecureTextEntryView * _Nonnull secureView) {
//        @strongify(self);
    [self topUpRequestWithMoney:topUpMoney];
//    }];
}
#pragma mark - private method

//充值
- (void)topUpRequestWithMoney:(NSString *)money{
   
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    TopUpMoneyParam *param = [[TopUpMoneyParam alloc] init];
    param.payType = @(self.payType);
    param.memberId = memberId;
    param.money = money;
    @weakify(self);
    [[[PigMarketApi sharedInstance] topUpRequestWithParam:param] subscribeNext:^(Pay * _Nullable pay) {
        
        
        @strongify(self);
        //调取支付宝和微信SDK
        [self thirdTopUpWithPayModel:pay memberId:memberId money:money];
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}


//调取支付宝和微信SDK
- (void)thirdTopUpWithPayModel:(Pay *)payModel memberId:(NSNumber *)memberid money:(NSString *)money{
    switch (self.payType) {
            
        case PayTypeAlipay:
        {
            //支付宝SDK
            @weakify(self);
            [[PayManager sharedManager] payWithAlipayMethod:payModel.payRequest callBack:^(PayErrCode errCode, NSString *errStr) {
                @strongify(self);
                if (errCode == PayErrCodeSuccess) {
                    //成功，跳充值成功页面
                    [self pushPayReaultVCWithMoney:money];
                } else {
                    [self toastWithContent:errStr];
                }
            }];
        }
            break;
        case PayTypeWechat:
        {
            //微信支付SDK
            @weakify(self);
            [[PayManager sharedManager] payWithWeChatMethod:payModel callBack:^(PayErrCode errCode, NSString *errStr) {
                @strongify(self);
                if (errCode == PayErrCodeSuccess) {
                    //成功，跳充值成功页面
                    [self pushPayReaultVCWithMoney:money];
                } else {
                    [self toastWithContent:errStr];
                }
            }];
        }
            break;
        default:
            break;
    }
}


- (void)pushPayReaultVCWithMoney:(NSString *)money {
    TopUpResultViewController *vc = [[TopUpResultViewController alloc] init];
    vc.topupMoney = money;
    if (self.payType == PayTypeWechat) {
        vc.payType = @"微信";
    } else {
        vc.payType = @"支付宝";
    }
    [self.navigationController presentViewController:vc animated:YES completion:nil];

}


#pragma mark - getter and setter
- (void)setPrice:(NSString *)price{
    _price = price;
    self.topUpView.moneyInputField.text = price;

}

- (TopUpAndWithdrawView *)topUpView {
    if (!_topUpView) {
        _topUpView = [[TopUpAndWithdrawView alloc] initWithType:@"充值"];
    }
    return _topUpView;
}

@end
